package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.MsgAdapter;
import com.dlwx.parttimetalent.adapter.WorkExperienceAdapter;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.bean.MsgBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MsgActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    private MsgAdapter mAdapter;
    private int type;
    private int page=1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg);
        ButterKnife.bind(this);
        type=getIntent().getIntExtra("type",0);
        initView();
        getDatas();
    }

    private void getDatas() {
        OkgoUtil.post(Http.Msglist, MsgActivity.this)
                .params("pageno",page)
                .params("msgtype",type)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        MsgBean parentBean = DealResult.getInstance().dealDatas(MsgActivity.this
                                , response, new TypeToken<MsgBean>() {
                                }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        int code = parentBean.getCode();
                        if (code == Constant.SUCESS) {
                            setDatas(parentBean.getBody());
                        } else {
                            if(code==Constant.EMPTY){
                                recyclerView.setNoMore(true);
                            }else{
//                                ToastUtils.toastForShort(getActivity(),parentBean.getResult());
                            }
                        }
//                        ToastUtils.toastForShort(MsgActivity.this, parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        loadingDialog.dismiss();
                        super.onError(response);
                        recyclerView.refreshComplete(mAdapter.getItemCount());
                        ToastUtils.toastForShort(MsgActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(List<MsgBean.BodyBean> body) {
        mAdapter.addAll(body);
        recyclerView.refreshComplete(mAdapter.getItemCount());
    }


    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        switch (type){
            case Constant.MSG_SYS:
                tvMiddleTitle.setText("系统消息");
                break;
            case Constant.MSG_ORDER:
                tvMiddleTitle.setText("订单消息");
                break;
            case Constant.MSG_TRADE:
                tvMiddleTitle.setText("交易消息");
                break;
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter=new MsgAdapter(this,type);

        mAdapter.setDataList(new ArrayList<MsgBean.BodyBean>());
        final LRecyclerViewAdapter lRecyclerViewAdapter=new LRecyclerViewAdapter(mAdapter);
        //divider
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.size_1)
//                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.bg)
                .build();
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        recyclerView.setLoadMoreEnabled(true);
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                page=1;
                mAdapter.clear();
                lRecyclerViewAdapter.notifyDataSetChanged();
                getDatas();
            }
        });
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getDatas();
            }
        });
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if(type==Constant.MSG_ORDER){
                    Intent i = new Intent(MsgActivity.this, MainActivity.class);
                    i.putExtra("type",1);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }else{
                    Intent i=new Intent(MsgActivity.this, MsgDetailActivity.class);
                    i.putExtra("type",type);
                    i.putExtra("content",mAdapter.getDataList().get(position).getMsginfo());
                    startActivity(i);
                }
            }
        });
        mAdapter.setOnDelListener(new WorkExperienceAdapter.onSwipeListener() {
            @Override
            public void onDel(int pos) {
                deleteWork(pos);
            }

            @Override
            public void onTop(int pos) {

            }
        });
    }
    private void deleteWork(final int pos) {
        loadingDialog.show();
        String msgid=mAdapter.getDataList().get(pos).getMsgid();
        OkgoUtil.post(Http.Delmsglist, MsgActivity.this)
                .params("msgid",msgid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(MsgActivity.this, response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            mAdapter.getDataList().remove(pos);
                            mAdapter.notifyItemRemoved(pos);//推荐用这个
                            if(pos != (mAdapter.getDataList().size())){ // 如果移除的是最后一个，忽略 注意：这里的mDataAdapter.getDataList()不需要-1，因为上面已经-1了
                                mAdapter.notifyItemRangeChanged(pos, mAdapter.getDataList().size() - pos);
                            }
                        }else{
                            ToastUtils.toastForShort(MsgActivity.this, parentBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(MsgActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_middle_title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_middle_title:
                break;
        }
    }
}
